package com.achartengine.test.tools;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: dengdayi
 * Date:2016/1/11 16:04
 * Description: 时间分布图工具类
 */
public class TimeFenGraphUtils {
    private static TimeFenGraphUtils mGraphUtils;

    /**
     * 获取实例
     *
     * @return
     */
    public static TimeFenGraphUtils getInstance() {
        if (mGraphUtils == null) {
            mGraphUtils = new TimeFenGraphUtils();
        }
        return mGraphUtils;
    }

    public static View getTimeChartView(Context context, List<double[]> mDoubleList, String format) {
        XYMultipleSeriesRenderer mRenderer = getSeriesRenderer();

        XYMultipleSeriesDataset mDataset = getSeriesDataset(mDoubleList);
        return ChartFactory.getTimeChartView(context, mDataset, mRenderer, format);
    }

    private static XYMultipleSeriesRenderer getSeriesRenderer() {
        XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();

        XYSeriesRenderer r = new XYSeriesRenderer();

        r.setColor(Color.RED);
        r.setPointStyle(PointStyle.CIRCLE);
        mRenderer.addSeriesRenderer(r);

        mRenderer.setChartTitle("时间分布图");//图表标题
        mRenderer.setXTitle("时间");//x轴说明
        mRenderer.setYTitle("成绩");//y轴说明

        mRenderer.setYAxisMax(100);              //y数轴上限
        mRenderer.setYAxisMin(0);              //y数轴下限

        mRenderer.setYAxisMax(12);              //x数轴上限
        mRenderer.setYAxisMin(1);              //x数轴下限

        mRenderer.setPanEnabled(false, false);    //曲线是否是可滑动触摸的

        mRenderer.setLabelsTextSize(30); // 设置轴标签文本大小
        mRenderer.setLegendTextSize(30); // 设置图例文本大小

//        mRenderer.setMargins(new int[]{20, 30, 15, 15}); // 设置4边留白

        mRenderer.setPanEnabled(false, false); // 设置x,y坐标轴不会因用户划动屏幕而移动

        mRenderer.setBackgroundColor(Color.TRANSPARENT); // 设置背景色透明

        //颜色设置
//        mRenderer.setYLabelsColor(0, Color.BLACK);
//        mRenderer.setAxesColor(Color.BLACK);
//        mRenderer.setXLabelsColor(Color.BLACK);
//        mRenderer.setYLabelsAlign(Paint.Align.RIGHT);

        mRenderer.setXLabelsAlign(Paint.Align.CENTER);// 刻度线与刻度标注之间的相对位置关系(X轴)
        mRenderer.setYLabelsAlign(Paint.Align.CENTER);// 刻度线与刻度标注之间的相对位置关系(Y轴)


        return mRenderer;
    }

    private static XYMultipleSeriesDataset getSeriesDataset(List<double[]> mDoubleList) {
        XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();

        List<double[]> x = new ArrayList<>();
        x.add(new double[]{});

        XYSeries series = new XYSeries("成绩-时间");

        double[] xV = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
        double[] xY = mDoubleList.get(0);

        int seriesLength = xV.length;

        for (int i = 0; i < seriesLength; i++) {
            series.add(xV[i], xY[i]);
        }
        mDataset.addSeries(series);

        return mDataset;
    }

    public static void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues,
                                   List<double[]> yValues, int scale) {
        int length = titles.length;
        for (int i = 0; i < length; i++) {
            XYSeries series = new XYSeries(titles[i], scale);
            double[] xV = xValues.get(i);
            double[] yV = yValues.get(i);
            int seriesLength = xV.length;
            for (int k = 0; k < seriesLength; k++) {
                series.add(xV[k], yV[k]);

            }
            dataset.addSeries(series);
        }
    }

}
